home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 4
/
Apprentice-Release4.iso
/
Languages
/
CGI shell
/
webTTT.4th
< prev
next >
Wrap
Text File
|
1995-09-18
|
3KB
|
112 lines
\
\ Web TIC TAC TOE in Forth. Mindless, for demonstration purposes only.
\
\ Ronald T. Kneusel, 18-Sep-95
\ rkneusel@post.its.mcw.edu
\
\ This code requires the CGIshell.4th file.
\
( load the CGI shell code )
( --> CGIshell.4th )
( *********************** Strings and such ************************* )
2048 String>> output \ put the reply here
5 String>> choice \ user's move here
message[ s0 <h2>Web Tic Tac Toe - A CGI demo in Forth</h2><hr size=2>]
message[ s1 <b>Your move....</b><p>]
message[ s2 <b>Game over, thanks for playing</b><p>]
message[ s3 <b>Illegal move, try again...</b><p>]
message[ bd <tt>1 | 2 | 3</tt><br><tt>4 | 5 | 6</tt><br><tt>7 | 8 | 9</tt>]
message[ c1 <select name="c"><option>1<option>2<option>3<option>4<option>5]
message[ c2 <option>6<option>7<option>8<option>9</select>]
message[ f1 <form method=post action="ttt.cgi">]
message[ f2 </form>]
message[ go <input type=submit value="GO">]
message[ fn c] \ Field name
\ offsets into the board string
create offset 4 , 8 , 12 , 26 , 30 , 34 , 48 , 52 , 56 ,
( *********************** Play TIC TAC TOE ************************* )
: <> = 0= ; macro
variable n
: random ( n -- n' ) ( puts a random number from 1 to n on stack )
n ! 0 >r ,$ A861 r> abs n @ 32767 */ 1+ ;
: set ( v n -- ) \ set value for position n
1- 2* offset + @ bd + c! ;
: get ( n -- val ) \ return value of position n
1- 2* offset + @ bd + c@ ;
: over? ( -- t|f ) \ true if the game is over (all spaces filled)
-1
10 1 DO
r get 88 <>
r get 79 <> and IF drop 0 THEN
LOOP ;
variable ILLEGAL 0 ILLEGAL ! \ true if illegal move
variable GOODBYE 0 GOODBYE ! \ true if game over
: make_a_move \ chose a random position
88 choice c@ 48 - dup
get dup 88 = swap 79 = or IF
drop -1 ILLEGAL ! \ bogus move
ELSE
set \ mark user's choice
over? 0= IF
9 random \ get a position
BEGIN dup dup get 88 =
swap get 79 = or WHILE drop 9 random REPEAT \ chose until open
79 swap set \ set to 'O'
over? IF -1 GOODBYE ! THEN
ELSE
-1 GOODBYE !
THEN
THEN
;
( ********************* Apple Event Handler ************************ )
,s sdoc ,s WWWΩ ae:
output newstr \ clear the output
output startstring \ new HTML string
s0 output strcpy \ header
choice fn NEW @Field \ get user's move
make_a_move \ then computer's turn
GOODBYE @ IF
s2 output strcpy \ game over
bd output strcpy
ELSE
ILLEGAL @ IF
s3 output strcpy \ illegal move
ELSE
s1 output strcpy \ valid moves
THEN
bd output strcpy
f1 output strcpy
c1 output strcpy
c2 output strcpy
go output strcpy
f2 output strcpy
THEN
output endstring
output REPLY \ send the reply
GOODBYE @ IF bye THEN \ quit if game over
;ae